package com.quktzhuanka.app.fragments;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.activity.FeedBackActivity;
import com.quktzhuanka.app.activity.MallGoodsDetailsActivity;
import com.quktzhuanka.app.activity.PromotionDetailsActivity;
import com.quktzhuanka.app.activity.WebViewActivity3;
import com.quktzhuanka.app.adapter.ShopMallGoodsRecyclerAdapter;
import com.quktzhuanka.app.base.BaseLazyFragment;
import com.quktzhuanka.app.bean.PromotionDetailsBean;
import com.quktzhuanka.app.bean.Response;
import com.quktzhuanka.app.bean.ShopMallGoodsBean;
import com.quktzhuanka.app.common.T;
import com.quktzhuanka.app.config.Constants;
import com.quktzhuanka.app.https.HttpUtils;
import com.quktzhuanka.app.https.onOKJsonHttpResponseHandler;
import com.quktzhuanka.app.my.RechargeActivity;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;


public class HkFragment extends BaseLazyFragment {
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.fragment)
    FrameLayout mFrameLayout;
    @BindView(R.id.wv)
    WebView wv;
    private View view;

    int startX = 0;
    @Override
    protected void lazyload() {

    }
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.ac_webview, container, false);
        ButterKnife.bind(this, view);
        view.findViewById(R.id.img_feed);

        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText(getArguments().getString("title"));
        wv.setWebViewClient(webViewClient);
        wv.setWebChromeClient(new HkFragment.MyWebChromeClient());
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js
        //支持屏幕缩放
        webSettings.setDomStorageEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setAllowFileAccess(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportMultipleWindows(false);//这里一定得是false,不然打开的网页中，不能在点击打开了
        // webSetting.setLoadWithOverviewMode(true);
        webSettings.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setAppCacheMaxSize(Long.MAX_VALUE);
//        webSettings.setAppCachePath(this.getDir("appcache", 0).getPath());
//        webSettings.setDatabasePath(this.getDir("databases", 0).getPath());
//        webSettings.setGeolocationDatabasePath(this.getDir("geolocation", 0)
//                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        wv.loadUrl(getArguments().getString("url"));


//        wv.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int pointX = (int) event.getX();
//                if (startX > pointX + 200){ // 左滑
//                    startX = pointX;
//                    if (HkFragment.this.wv.canGoBack()){
//                        HkFragment.this.wv.goBack();
//                    }
//                }
////                else if(startX < pointX - 200) {
////                    startX = pointX;
////                    if (HkFragment.this.wv.canGoForward()){
////                        HkFragment.this.wv.goForward();
////                    }
////                }
//
//                return false;
//            }
//        });
        return view;
    }



//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (wv.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK) {//点击返回按钮的时候判断有没有上一页
//            wv.goBack(); // goBack()表示返回webView的上一页面
//            return true;
//        }else {
//
//        }
//        return super.onKeyDown(keyCode, event);
//    }


    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {//页面加载完成
            if(!url.contains("oauth.taobao")&&!url.contains("oauth.m.taobao")&&!url.contains("trip"))
                closeLoadingDialog();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载
            if(!url.contains("oauth.taobao")&&!url.contains("oauth.m.taobao")&&!url.contains("trip"))
                showLoadingDialog();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String newurl) {
            if(newurl.contains("detail.m.tmall")||newurl.contains("detail.tmall.hk")){
                Uri uri = Uri.parse(newurl);
                String numId=uri.getQueryParameter("id");
//                getGoodsMsgRequest(numId);
                return true;
            }
            if(newurl.contains("trip/travel-detail")){
                Uri uri = Uri.parse(newurl);
                String numId=uri.getQueryParameter("id");
//                getGoodsMsgRequest(numId);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, newurl);
        }
    };

    private class MyWebChromeClient extends WebChromeClient {
        private View mCustomView;
        private CustomViewCallback mCustomViewCallback;
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }
            mCustomView = view;
            mFrameLayout.addView(mCustomView);
            mCustomViewCallback = callback;
            wv.setVisibility(View.GONE);
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        public void onHideCustomView() {
            wv.setVisibility(View.VISIBLE);
            if (mCustomView == null) {
                return;
            }
            mCustomView.setVisibility(View.GONE);
            mFrameLayout.removeView(mCustomView);
            mCustomViewCallback.onCustomViewHidden();
            mCustomView = null;
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            super.onHideCustomView();
        }
    }
    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        switch (config.orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
//                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                break;
            case Configuration.ORIENTATION_PORTRAIT:
//                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                break;
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        wv.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        wv.onResume();

//        getView().setFocusableInTouchMode(true);
//        getView().requestFocus();
//        getView().setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View view, int i, KeyEvent keyEvent) {
//                if (wv.canGoBack() && keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) {//点击返回按钮的时候判断有没有上一页
//                    wv.goBack(); // goBack()表示返回webView的上一页面
//                    return true;
//                }
//                return super.onKeyDown(keyCode, event);
//            }
//        });

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        wv.destroy();
    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        if(this.wv.canGoBack()){
            wv.goBack();
        }
    }

}
